package interactor;

import context.*;

import mainFrame.DrawPanel;

import java.awt.Graphics2D;
import java.awt.image.ImageObserver;

import object2D.Object2D;
import util.PixelPoint;

// ********************************************************************
// *** CLASS: EditInteractor
// ********************************************************************
public class EditInteractor extends Interactor implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  static final String TAG="interactor.EditInteractor";

  public String getMouseModeMnemonic() { return("Edit"); }
  private Object2D.PointMark draggingMark; // !null when dragging a PointMark
  private Object2D draggingObject;    // !null when dragging a DrawObject

  // ******************************************************************
  // *** CONSTRUCTOR: EditInteractor
  // ******************************************************************
  public EditInteractor(DrawPanel drawPanel)
  { super(drawPanel,EDIT); }

  // ******************************************************************
  // *** METHOD: paintMouseEffect
  // ******************************************************************
  public void paintMouseEffect(Graphics2D g,ImageObserver imageObserver)
  {	if(draggingObject!=null)
	{ double dx=mouseCurrent.getX()-mouseStart.getX();
	  double dy=mouseCurrent.getY()-mouseStart.getY();
	  draggingObject.paintContour((Graphics2D)g,dx,dy);
	}
  }

  // ******************************************************************
  // *** METHOD: mouseDragged
  // ******************************************************************
  // Invoked when a mouse button is pressed and then dragged.
  protected void mouseDragged()
  { if(draggingObject!=null) drawPanel.repaint();
    if(draggingMark!=null) { draggingMark.moveTo(mouseCurrent.getX(),mouseCurrent.getY()); drawPanel.repaint(); }
  }

  // ******************************************************************
  // *** METHOD: mouseMoved
  // ******************************************************************
  // Invoked when the mouse has been moved (with no buttons down).
  protected void mouseMoved() {}

  // ******************************************************************
  // *** METHOD: mousePressed
  // ******************************************************************
  // Invoked when a mouse button has been pressed.
  protected void mouseLeftPressed(PixelPoint p)
  { if(draggingMark==null && draggingObject==null)
    { mouseStart=p;
      draggingMark=drawPanel.getPointMark(mouseCurrent);
	  if(draggingMark==null) draggingObject=drawPanel.getObject(mouseCurrent);
    }
  }
  protected void mouseRightPressed(PixelPoint p) {}

  // ******************************************************************
  // *** METHOD: mouseReleased
  // ******************************************************************
  // Invoked when a mouse button has been released.
  protected void mouseLeftReleased(PixelPoint p)
  { if(draggingObject!=null)
	{ double dx=mouseCurrent.getX()-mouseStart.getX();
	  double dy=mouseCurrent.getY()-mouseStart.getY();
      draggingObject.move(dx,dy);
      draggingObject=null;
	  drawPanel.repaint();
    }
    draggingMark=null;
  }
  protected void mouseRightReleased(PixelPoint p)
  { Object2D.PointMark mark=drawPanel.getPointMark(p);
    if(mark!=null) 
    { if(DEBUG) Log.d(TAG,"mouseRightReleased: WithinMark="+mark);
      mark.edit();	
    }
    else
    { Object2D obj=drawPanel.getObject(p);
      if(obj!=null) 
      { if(DEBUG) Log.d(TAG,"mouseRightReleased: WithinObject="+obj);
        drawPanel.setSelected(obj);
        obj.edit();
      }
    }
  }

}